package common

import (
	"fmt"
	"io"
	"os"
	"path"
	"path/filepath"
	"runtime"

	nested "github.com/antonfisher/nested-logrus-formatter"
	log "github.com/sirupsen/logrus"
)

// ------------------------------------------------------------------------------
//
// ------------------------------------------------------------------------------
func InitLog() {
	file, err := os.OpenFile(filepath.Join(filepath.Dir(os.Args[0]), "dolphin.log"),
		os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
	if nil != err {
		return
	}

	log.SetFormatter(&nested.Formatter{
		HideKeys:    false,
		CallerFirst: true,
		FieldsOrder: []string{"component", "category"},
		CustomCallerFormatter: func(f *runtime.Frame) string {
			fileName := path.Base(f.File)
			lastPath := path.Base(path.Dir(f.File))
			return fmt.Sprintf(" [%s:%d]", lastPath+"/"+fileName, f.Line)
		},
	})
	log.SetReportCaller(true)
	log.SetOutput(io.MultiWriter(os.Stdout, file))
	log.SetLevel(log.TraceLevel)
}
